package com.borderbears.backend.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ConfirmDialog extends JDialog {

	public static int APPROVE_OPTION = 1;
	public static int CANCEL_OPTION = 2;
	private final boolean userHasAccepted = false;
	private ValidatingPanel validatingPanel;

	private final List<ActionListener> listeners = new ArrayList<ActionListener>();

	public boolean isUserHasAccepted() {
		return userHasAccepted;
	}

	public ConfirmDialog(final Frame owner, final String title,
			final ModalityType type) {
		super(owner, title, type);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(400, 300);
		setLocationRelativeTo(owner);
		setLayout(new BorderLayout());

		final JButton btCancel = new JButton("Discard");
		btCancel.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				setVisible(false);
			}
		});
		final JButton btOK = new JButton("Accept");
		btOK.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				if (validatingPanel.isValid()) {
					fireActionPerformed();
					setVisible(false);
				}
			}
		});

		final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.add(btCancel);
		buttonPanel.add(btOK);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public void setContent(final ValidatingPanel comp) {
		validatingPanel = comp;
		add(validatingPanel, BorderLayout.CENTER);
	}

	public void addActonListener(final ActionListener l) {
		if (!listeners.contains(l)) {
			listeners.add(l);
		}
	}

	public void removeActionListener(final ActionListener l) {
		listeners.remove(l);
	}

	public void fireActionPerformed() {
		final ActionEvent ev = new ActionEvent(this, 0, "");
		for (final ActionListener l : listeners) {
			l.actionPerformed(ev);
		}
	}

}
